JSTestRunner = function(){
   
    var _myTestStore   = null;    
    var _testFileField = null;
    var _tp            = null;   
       
    return {
        
        init : function(){
            
            Ext.QuickTips.init();
            
            _testFileField = new Ext.form.TextField({
			                    width: 600,
			                    emptyText:'Type the url of your testsuite here...',
			                    listeners:{
				                    render: function(f){
					                    //f.el.on('keydown', filterTree, f, {buffer: 350});
				                    }
			                    }
		                     });

            var hd = new Ext.Panel({
                border: false,
                layout:'anchor',
                region:'north',
                cls: 'docs-header',
                height:60,
                
                items: [
                {
                    xtype:'box',
                    el:'header',
                    border:false,
                    anchor: 'none -25'
                },
                
                new Ext.Toolbar({
                    cls:'top-toolbar',
                    items:[ 
                        ' ',
			            {
                            iconCls: 'icon-expand-all',
				            tooltip: 'Expand All',
                            handler: function(){ 
                                _tp.root.expand(true); 
                            }
                        },
                        '-',
                        {
                            iconCls: 'icon-collapse-all',
                            tooltip: 'Collapse All',
                            handler: function(){ 
                                _tp.root.collapse(true); 
                            }
                        },
                        '-',            
                        _testFileField,
		                new Ext.Toolbar.Button({
		                    text : 'Load',
		                    tooltip  : 'Load the specified test suite',
		                    handler : this.load,
		                    scope : this
		                }),
		                '-',
		                new Ext.Toolbar.Button({
		                    text : 'Run',
		                    tooltip  : 'Run the specified test suite',
		                    handler  : this.run,
		                    scope    : this
		                })/*,
		                
		                '-',		                
		                new Ext.Toolbar.Button({
		                    text : 'Stop',
		                    tooltip  : 'Stop the specified test suite',
		                    handler  : this.stop,
		                    scope    : this
		                }),
		                '-',
		                new Ext.ProgressBar({
		                    width : 300
		                }),
		                '-',
		                new Ext.Toolbar.TextItem('Passed:'),		            		                
		                '-',
		                new Ext.Toolbar.TextItem('Failed:'),		            
		                '-',
		                new Ext.Toolbar.TextItem('Error:')		                    
                        */
                    ]
                })]
            });
                        
            _myTestStore = new Ext.data.Store({
                reader: new Ext.data.ArrayReader({}, [
                       {name: 'testCase'},
                       {name: 'status'},
                       {name: 'timeTaken', type: 'float'}
                ])
            });
                        
            var mp = new Ext.grid.GridPanel({
                ds: _myTestStore,
                region : 'center',
                frame : true,
                columns: [
                    {id:'testCase', header: "Test", width: 400, sortable: true, dataIndex: 'testCase'},
                    {header: "Status", /*width: 120,*/ sortable: true, dataIndex: 'status'},
                    {header: "Time taken", /*width: 120,*/ sortable: true, dataIndex: 'timeTaken'}
                ]        
            });
                        
            _tp = new Ext.tree.TreePanel({
                id:'api-tree',
                region:'west',
                split:true,
                width: 280,
                minSize: 175,
                maxSize: 500,
                collapsible: true,
                margins:'0 0 5 5',
                cmargins:'0 0 0 0',
                rootVisible:true,
                lines:false,
                autoScroll:true,
                animCollapse:false,
                animate: false,
                collapseMode:'mini',
            
                loader: new Ext.tree.TreeLoader({
			        preloadChildren: false,
			        clearOnLoad: false
		        }),    
            
                root: new Ext.tree.TreeNode({
                    text:'JSUnit Tests',
                    id:'root',
                    checked :true,   
                    listeners: {
                       'checkchange' : function(node,checked){    
                            if(node.childNodes){                
                                for(var i=0;i<node.childNodes.length;i++){
                                    node.childNodes[i].fireEvent('checkchange',node.childNodes[i],checked);
                                }     
                            }
                       }
                    }                    
                }),
                                 
                collapseFirst:false
            });

            var viewport = new Ext.Viewport({
                layout:'border',
                items:[hd,_tp,mp]
            });
            
            viewport.doLayout();
                       
            setTimeout(function(){
                Ext.get('loading').remove();
                Ext.get('loading-mask').fadeOut({remove:true});
            }, 250);                       
            
        }, // init    
        
        load : function(){          
            _myTestStore.removeAll();
            
            var parent = _tp.getRootNode();
            
            if(parent.childNodes){
                for(var i=0;i<parent.childNodes.length;i++){
                    parent.removeChild(parent.childNodes[i]);
                }
            }
                        
            if (!top.testManager) {
                top.init();
            }

            if (Utilities.isBlank(top.testManager.getTestFileName())) {
                top.testManager.fatalError('No Test Page specified.');
                return;
            }

            top.testManager.setup();

            top.testManager._currentSuite().addTestPage(top.testManager.resolveUserEnteredTestFileName());
            top.tracer.initialize();            
            
            top.testManager.start(false);
            
        },
        
        shouldRunSetUpPage : function(parent){
            
            for(var i=0;i<parent.childNodes.length;i++){
                if(parent.childNodes[i].getUI().isChecked()){                    
                    return true;
                }
            }
            
            return false;
        },
        
        shouldRunTest : function(parent,name){
        
            for(var i=0;i<parent.childNodes.length;i++){
                if(parent.childNodes[i].text == name){
                    return parent.childNodes[i].getUI().isChecked();
                }
            }
            
            return false;
        },
        
        getSuiteNode : function(name){
            var parent = _tp.getRootNode();
            
            for(var i=0;i<parent.childNodes.length;i++){
                if(parent.childNodes[i].text == name){                    
                    return parent.childNodes[i];
                }
            }
        },
        
        appendSuiteNode : function(name){            
            var rootNode = _tp.getRootNode();
            
            return rootNode.appendChild(new Ext.tree.TreeNode({
                text : name,
                checked : true,
                listeners: {
                   'checkchange' : function(node,checked){
                        node.getUI().toggleCheck(checked);
                        if(node.childNodes){                
                            for(var i=0;i<node.childNodes.length;i++){
                                node.childNodes[i].fireEvent('checkchange',node.childNodes[i],checked);
                            }     
                        }
                   }
                }     
            }));
        },
        
        appendTestNode : function(parent,name){
            
            parent.appendChild(new Ext.tree.TreeNode({
                text : name,
                checked : true,
                leaf : true,
                listeners: {
                   'checkchange' : function(node,checked){
                        node.getUI().toggleCheck(checked);                        
                   }
                }
            }));
        },
            
        run : function(){
            
            _myTestStore.removeAll();
            
            if (!top.testManager) {
                top.init();
            }

            if (Utilities.isBlank(top.testManager.getTestFileName())) {
                top.testManager.fatalError('No Test Page specified.');
                return;
            }

            top.testManager.setup();

            top.testManager._currentSuite().addTestPage(top.testManager.resolveUserEnteredTestFileName());
            top.tracer.initialize();
            
            top.testManager.start(true);            
        },        
        
        getTestFileName : function(){
            return _testFileField.getValue();
        },
        
        updateGrid : function(data){
            _myTestStore.loadData(data,true);
        }
    };
    
}();

Ext.EventManager.onDocumentReady(JSTestRunner.init, JSTestRunner, true);